package com.lw.callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author LW
 * @2020/8/20 8:39
 */
public class CallableTest {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
//        new Thread(new Runnable()).start();
//        Runnable和Callable共有的实现类FutureTask
//        new Thread().start();//怎么启动Callable
        MyThread myThread = new MyThread();
        FutureTask futureTask = new FutureTask<>(myThread);//适配类
        new Thread(futureTask,"A").start();
        new Thread(futureTask,"B").start();//细节*****   结果会被缓存，效率高
        String o = (String) futureTask.get();//细节******这个get方法可能会产生阻塞！把他放到最后
        //或者使用异步通信来处理
        System.out.println(o);//返回Callable的返回 结果
    }
}

class MyThread implements Callable<String>{

    @Override
    public String call() throws Exception {
        System.out.println("call()");
        //耗时的操作
        return "123";
    }
}